package com.slofzx.nuoda.evaluation.param.service.impl;

import com.slofzx.nuoda.common.enums.CommonResultEnum;
import com.slofzx.nuoda.common.enums.ServiceResult;
import com.slofzx.nuoda.common.exception.ServiceException;
import com.slofzx.nuoda.evaluation.param.dao.CommonParamVersionDAO;
import com.slofzx.nuoda.evaluation.param.dto.CommonParamVersionDataDTO;
import com.slofzx.nuoda.evaluation.param.entity.CommonParamVersion;
import com.slofzx.nuoda.evaluation.param.enums.ParamVersionStateEnum;
import com.slofzx.nuoda.evaluation.param.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 成本取值版本数据存取实现逻辑
 * @author sunhaifeng
 */
@Service
public class CommonParamVersionDataServiceImpl implements CommonParamVersionDataService {

    /**
     * 单位相关数据服务
     */
    @Autowired
    protected CommonUnitCostService commonUnitCostService;
    /**
     * 固定参数数据服务
     */
    @Autowired
    protected CommonFixedService commonFixedService;
    /**
     * 基准收益率数据服务
     */
    @Autowired
    protected CommonBaseIrrService commonBaseIrrService;
    /**
     * 年度相关数据服务
     */
    @Autowired
    protected CommonYearRelatedService commonYearRelatedService;
    @Resource
    protected CommonParamVersionDAO commonParamVersionDAO;

    @Override
    @Transactional
    public ServiceResult save(CommonParamVersionDataDTO dto) {
        String versionId = dto.getVersionId();
        CommonParamVersion version = commonParamVersionDAO.selectById(versionId);
        if(version == null) {
            return CommonResultEnum.NOT_EXIST;
        }
        if(ParamVersionStateEnum.getEnum(version.getState()) == ParamVersionStateEnum.PUBLISH) {
            return CommonResultEnum.PERMISSION_DENY;
        }

        ServiceResult result = commonUnitCostService.save(versionId,dto.getUnitCostData());
        if(result!=CommonResultEnum.SUCCESS) {
            throw new ServiceException("保存单位相关数据失败","保存公共参数——单位相关数据失败",dto.getUnitCostData());
        }
        result = commonFixedService.save(dto.getFixedData());
        if(result!=CommonResultEnum.SUCCESS) {
            throw new ServiceException("保存固定参数失败","保存公共参数——固定参数数据失败",dto.getFixedData());
        }
        result = commonBaseIrrService.save(versionId,dto.getBaseIrrData());
        if(result!=CommonResultEnum.SUCCESS) {
            throw new ServiceException("保存基准收益率数据失败","保存公共参数——基准收益率数据失败",dto.getBaseIrrData());
        }
        result = commonYearRelatedService.save(versionId,dto.getYearRelatedData());
        if(result!=CommonResultEnum.SUCCESS) {
            throw new ServiceException("保存年度相关数据失败","保存公共参数——年度相关数据失败",dto.getYearRelatedData());
        }

        return CommonResultEnum.SUCCESS;
    }

    /**
     * 此处由其他地方控制权限，不做校验
     * @param versionId
     * @return
     */
    @Transactional
    @Override
    public ServiceResult delete(String versionId) {
        commonUnitCostService.deleteByVersion(versionId);
        commonFixedService.deleteByVersion(versionId);
        commonYearRelatedService.deleteByVersion(versionId);
        commonBaseIrrService.deleteByVersion(versionId);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    public void copy(String fromId, String toId) {
        commonBaseIrrService.copy(fromId,toId);
        commonFixedService.copy(fromId,toId);
        commonUnitCostService.copy(fromId,toId);
        commonYearRelatedService.copy(fromId,toId);
    }
}
